package com.xiaoshuidi.cloud.module.contract.service.fee;

import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.util.TenantUtils;
import com.xiaoshuidi.cloud.module.contract.controller.admin.fee.vo.FeeTypesCreateReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.fee.vo.FeeTypesUpdateReqVO;
import com.xiaoshuidi.cloud.module.contract.convert.fee.FeeTypesConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.fee.FeeTypesDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.fee.FeeTypesMapper;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.util.Pinyin4jUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants.FEE_TYPES_NOT_EXISTS;

/**
 * 基本费项 Service 实现类
 *
 * @author 合房租赁
 */
@Service
public class FeeTypesServiceImpl implements FeeTypesService {

    @Resource
    private FeeTypesMapper feeTypesMapper;

    @Override
    public FeeTypesDO createFeeTypes(FeeTypesCreateReqVO createReqVO) {
        String feeCode = Pinyin4jUtil.getPingYin(createReqVO.getFeeName())+ ApiConstants.FEES;
        // 插入
        FeeTypesDO feeTypes = FeeTypesConvert.INSTANCE.convert(createReqVO);
        feeTypes.setFeeCode(feeCode);
        feeTypesMapper.insert(feeTypes);

        // 返回
        return feeTypes;
    }

    @Override
    public void updateFeeTypes(FeeTypesUpdateReqVO updateReqVO) {
        // 校验存在
        validateFeeTypesExists(updateReqVO.getId());
        // 更新
        FeeTypesDO updateObj = FeeTypesConvert.INSTANCE.convert(updateReqVO);
        feeTypesMapper.updateById(updateObj);
    }

    @Override
    public void deleteFeeTypes(Long id) {
        // 校验存在
        validateFeeTypesExists(id);
        // 删除
        feeTypesMapper.deleteById(id);
    }

    private void validateFeeTypesExists(Long id) {
        if (feeTypesMapper.selectById(id) == null) {
            throw exception(FEE_TYPES_NOT_EXISTS);
        }
    }

    @Override
    public FeeTypesDO getFeeTypes(Long id) {
        return feeTypesMapper.selectById(id);
    }

    @Override
    @TenantIgnore
    public List<FeeTypesDO> getFeeTypesByName(String feeName,List<Long> tenantIds) {
        return feeTypesMapper.getFeeTypesByName(feeName,tenantIds);
    }


    @Override
    public FeeTypesDO getFeeTypes(String feeName) {
        Long tenantId = Objects.requireNonNull(SecurityFrameworkUtils.getLoginUser()).getTenantId();
        return TenantUtils.executeIgnoreAndReturn(()->feeTypesMapper.getFeeTypes(feeName,tenantId));
    }

    @Override
    public List<FeeTypesDO> getFeeTypesList(Collection<Long> ids) {
        return feeTypesMapper.selectBatchIds(ids);
    }



//    @Override
//    public PageResult<FeeTypesDO> getFeeTypesPage(FeeTypesPageReqVO pageReqVO) {
//        return feeTypesMapper.selectPage(pageReqVO);
//    }
//
//    @Override
//    public List<FeeTypesDO> getFeeTypesList(FeeTypesExportReqVO exportReqVO) {
//        return feeTypesMapper.selectList(exportReqVO);
//    }

}
